package me.imid.view;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.RelativeLayout.LayoutParams;

public class ExpandView {
	private boolean isOpened=false;
	int duration=200;
	private boolean isAnimationRunning=false;
	private List<View> listView=new ArrayList<View>();
	public void add(View v){
		if(!listView.contains(v))
			listView.add(v);
	}
	public void showSelf(final View v){
		for(View listv:listView)
			if(v!=listv)
				if(listv.getVisibility()==View.VISIBLE)
					collapse(listv);
		
		if(v!=null && v.getVisibility()==View.GONE)
			expand(v);
	}
	public void hideAllViews(){
		showSelf(null);
	}
	
	private void expand(final View v)
	{
		if(v.getVisibility()==View.VISIBLE) return;
        v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);

        Animation animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t)
            {
                if (interpolatedTime == 1)
                    isOpened = true;
                v.getLayoutParams().height = (interpolatedTime == 1) ? LayoutParams.WRAP_CONTENT : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }


            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        animation.setDuration(duration);
        v.startAnimation(animation);
    }

    private void collapse(final View v)
    {
        final int initialHeight = v.getMeasuredHeight();
        Animation animation = new Animation()
        {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                if(interpolatedTime == 1)
                {
                    v.setVisibility(View.GONE);
                    isOpened = false;
                }
                else{
                    v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        animation.setDuration(duration);
        v.startAnimation(animation);
    }

    public Boolean isOpened()
    {
        return isOpened;
    }

    public void show(View v)
    {
        if (!isAnimationRunning)
        {
            expand(v);
            isAnimationRunning = true;
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    isAnimationRunning = false;
                }
            }, duration);
        }
    }
    
    Iterator<View> iterator;
    private void resetIterator(){
    	 iterator=listView.iterator();
    }
    
    public void showAllViews(){
    	resetIterator();
    	showAll();
    }
    private void showAll()
    {
    	
    	if(iterator.hasNext()){
    		if (!isAnimationRunning)
            {
                expand(iterator.next());
                isAnimationRunning = true;
                new Handler().postDelayed(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        isAnimationRunning = false;
                        showAll();
                    }
                }, duration);
            }
    		
    	}

        
    }
    
    public void hide(View v)
    {
        if (!isAnimationRunning)
        {
            collapse(v);
            isAnimationRunning = true;
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    isAnimationRunning = false;
                }
            }, duration);
        }
    }
}
